package com.service.impl;

import com.dao.TabAddressDao;
import com.pojo.PageBean;
import com.pojo.TabAddress;
import com.service.TabAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class TabAddressServiceImpl implements TabAddressService {
    @Autowired
    private TabAddressDao tabAddressDao;

    @Override
    public PageBean findMyAddress(Integer current, Integer size, String contact, Integer uid,String address) {
        int begin= (current-1)*size;
        List<TabAddress> myAddressByUserId = tabAddressDao.findMyAddressByUserId(begin, size, contact, uid,address);
        System.out.println(myAddressByUserId);
        int count=tabAddressDao.getCount(contact,uid,address);
        return new PageBean(myAddressByUserId,count,current,size);
    }

    /**
     * 添加收获地址
     * @param tabAddress
     * @return
     */
    @Override
    public int addAddress(TabAddress tabAddress){
        System.out.println(tabAddress);
        return tabAddressDao.addAddress(tabAddress);
    }

    @Override
    public int updateAddress(TabAddress tabAddress) {
        return tabAddressDao.updateAddress(tabAddress);
    }

    @Override
    @Transactional
    public int updateDefultAddress(Integer aid,Integer uid) {
        int row2 = tabAddressDao.updateNotDefultAddress(uid);
        int row = tabAddressDao.updateDefultAddress(aid);
        return row>0&&row2>0?1:0;
    }

    @Override
    public int deleteAddress(Integer aid) {
        return tabAddressDao.deleteAddress(aid);
    }

}
